/**
* \file
*			Real-timer specific implementation for STM8L151C8.
* \author
*			JiangJun <jiangjunjie_2005@126.com>
*/

#include "sys/energest.h"
#include "sys/rtimer.h"
#include "rtimer-arch.h"
#include "stm32f1xx_hal.h"



#define DEBUG 0
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif

/*---------------------------------------------------------------------------*/
/**
 * Handle interrupt of real time timer.
 *
 * This function was invoked when timer of real time is expired.
 *
 */
void
rtimer_irq_handler(void)
{
    /* Clear interrupt pending bit */
//    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
//  
//    /* Do interrupt only once! */
//    TIM_ITConfig(TIM3, TIM_IT_CC1, DISABLE);

//    ENERGEST_ON(ENERGEST_TYPE_IRQ);
//    rtimer_run_next();
//    ENERGEST_OFF(ENERGEST_TYPE_IRQ);

    return;	
}

void TIM3_IRQHandler(void)
{
//  if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
//  {
//	  rtimer_irq_handler();
//  }
}

/*---------------------------------------------------------------------------*/
/**
 * Initialize timer for real time timer.
 *
 * This function would initialize TIM1 for real time timer.
 *
 */
void
rtimer_arch_init(void)
{
//	TIM_TimeBaseInitTypeDef TIM_InitStructure;
//	TIM_OCInitTypeDef  TIM_OCInitStructure;
//	NVIC_InitTypeDef NVIC_InitStructure;
//	
//	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//	
//	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
//	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIM_PreemptionPriority;
//	NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIM_SubPriority;
//	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//	NVIC_Init(&NVIC_InitStructure);
//  
//	TIM_DeInit(TIM3);
//    uint16_t PrescalerValue = (uint16_t) (SystemCoreClock / 1000 ) - 1;
//	/* Time base configuration */
//	TIM_InitStructure.TIM_Period = 0xffff;
//	TIM_InitStructure.TIM_Prescaler = 0;
//	TIM_InitStructure.TIM_ClockDivision = 0;
//	TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
//	TIM_TimeBaseInit(TIM3, &TIM_InitStructure);
//	
//	TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);
//	
//	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
//	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
//	TIM_OCInitStructure.TIM_Pulse = 1;
//	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//	TIM_OC1Init(TIM3, &TIM_OCInitStructure);

//	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
//  	
//	TIM_ClearFlag(TIM3, TIM_IT_CC1);
//    TIM_ITConfig(TIM3, TIM_IT_CC1, DISABLE);  
//    TIM_Cmd(TIM3, ENABLE);
// 
    return;
}

/**
 * \brief    Get the current clock time
 * \return  The current time
 *
 *            This function returns what the real-time module thinks
 *            is the current time. The current time is used to set
 *            the timeouts for real-time tasks.
 *
 * \hideinitializer
 */
rtimer_clock_t
rtimer_arch_now(void)
{
    rtimer_clock_t    tT1, tT2;

    do
    {
        /* Avoid race condition on reading counter of TIM1 */
        tT1 = TIM3->CNT;
        tT2 = TIM3->CNT;
    } while (tT1 != tT2);

    return tT1;
}

/**
 * \brief      Set an interrupt to timeout event.
 * \param    rtimer_clock_t t    the quantity of timeout, unit is ms.
 *
 *              This function schedules a real-time task at a specified
 *              time in the future.
 */
void
rtimer_arch_schedule(rtimer_clock_t t)
{
	
//    TIM_ClearFlag(TIM3, TIM_IT_CC1);
//	TIM_SetCompare1(TIM3,t+TIM_GetCapture1(TIM3));  //+TIM_GetCapture1(TIM3)
//    TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);
    return;
}

/**
 * \brief    Disable interrupt of rtimer timeout.
 *
 *            This function disable interrupt of real timer for removing timer.
 */
void
rtimer_arch_disable_irq(void)
{  
//    TIM_ITConfig(TIM3, TIM_IT_CC1, DISABLE);

    return;
}

/**
 * \brief    Enable interrupt of rtimer timeout.
 *
 *            This function enable interrupt of real timer for restarting timer.
 */
void
rtimer_arch_enable_irq(void)
{  
//    TIM_ClearFlag(TIM3, TIM_IT_CC1);

//    TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);

    return;
}

/**
 * \brief      turn off the real timer.
 * \param    none
 *
 *              This function would turn off the real timer for saved energy.
 */
void
rtimer_arch_TurnOff(void)
{
//    TIM_Cmd(TIM3, DISABLE);
//	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,DISABLE);

    return;
}

/**
 * \brief      turn on the real timer.
 * \param    none
 *
 *              This function would turn on the real timer.
 */
void
rtimer_arch_TurnOn(void)
{
//    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//    TIM_Cmd(TIM3, ENABLE);

    return;
}


/*--------------------------------------------------------------------------------------------------------
                   									     0ooo
                   								ooo0     (   )
                								(   )     ) /
                								 \ (     (_/
                								  \_)
----------------------------------------------------------------------------------------------------------*/

